//
//  SmokeController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/9.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "SmokeController.h"
#import "Config.h"

@interface SmokeController ()

@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UITextField *timeField;
@property (weak, nonatomic) IBOutlet UITextField *countField;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

@implementation SmokeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"吸烟危害自测";
    [self setViewAttrWithView:self.timeField];
    [self setViewAttrWithView:self.countField];
    self.button.layer.cornerRadius = 5.0f;
    self.button.layer.masksToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setViewAttrWithView:(UIView *)view {
    view.layer.cornerRadius = 5.0f;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 0.5f;
    view.layer.borderColor = XHConfigColor.CGColor;
}

#pragma mark - 查看结果
- (IBAction)buttonEvent:(UIButton *)sender {
    NSString *time = self.timeField.text;
    NSString *count = self.countField.text;
    if (time.length < 1)
        [self setAlertViewWithMessage:@"请输入抽烟时间(年)！"];
    else if (count.length < 1)
        [self setAlertViewWithMessage:@"请输入每天吸烟量(支)!"];
    else
        [self calculateResult];
}

- (void)calculateResult {
    [self.view endEditing:NO];
    NSInteger time = [self.timeField.text integerValue];
    NSInteger count = [self.countField.text integerValue];
    CGFloat result = time* 365 * count * 13.8 / (60 * 24);
    for (UIView *view in self.view.subviews)
        if (view != self.textLabel)
            view.hidden = YES;
    self.textLabel.text = [NSString stringWithFormat:@"您的烟龄为：%@年，每天吸烟量为：%@支，您的寿命减少了%.f天。", self.timeField.text, self.countField.text, result];
}

#pragma mark - AlertView
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:NO];
}

@end
